//
//  SearchOrderInputViewController.m
//  DPGJ
//
//  Created by ZhuoLaiQiang@Gmail.com on 11-11-28.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "SearchOrderInputViewController.h"
#import "AppDelegate.h"
#import "TempView.h"
#import "CONSTS.h"

static NSDate *s_startDate;

@interface SearchOrderInputViewController()

- (void)pickedDate;
- (void)selectOrderStatus;

@end

@implementation SearchOrderInputViewController

@synthesize delegate        =       _delegate;
@synthesize datePick = _datePick;
@synthesize textPick = _textPick;

- (void)dealloc 
{
    [_textPick release];
    [_datePick release];
    LF_RELEASE_SAFELY(_pickDataSource);
    
    [super dealloc];
}

- (id)initWithDelegate:(id<LFInputViewLayoutDelegate>)delegate_ layout:(LFInputViewLayout)layout_ row:(NSInteger)row_
{
    self = [super init];
    if (self) 
    {
        self.delegate = delegate_;
        _layoutSign = layout_;
        _row = row_;
    }
    
    return self;
}

void setStartDate(NSDate *date_)
{
    LF_RELEASE_SAFELY(s_startDate);
    s_startDate = date_;
}

- (void)loadView
{
    NSArray *viewArray = [[NSBundle mainBundle] loadNibNamed: @"SearchOrderInputViewController" owner: self options: nil];
    
    if (_layoutSign == LFInputViewLayoutForPickDate)
    {
        self.view = [viewArray objectAtIndex: 0];
        if (_row == 0) 
        {
            _datePick.maximumDate = [NSDate date];
            _datePick.minimumDate = [NSDate dateWithTimeIntervalSinceNow: -(60 * 60 * 24 * 30 * 3)];
        }else
        {
            _datePick.maximumDate = [NSDate date];
            _datePick.minimumDate = [s_startDate dateByAddingTimeInterval: 60 * 60 * 24];
        }
        
    }else if (_layoutSign == LFInputViewLayoutForOrderStatus)
    {
        /*
         #define              @"没有创建支付宝交易"
         #define                   @"等待买家付款"
         #define           @"等待卖家发货,即:买家已付款"
         #define         @"等待买家确认收货,即:卖家已发货"
         #define               @"买家已签收,货到付款专用"
         #define                   @"交易成功"
         #define                     @"付款以后用户退款成功，交易自动关闭"
         #define           @"付款以前，卖家或买家主动关闭交易"
         */
        if (_row == 2) 
        {
            _pickDataSource = [[NSArray alloc] initWithObjects: BU_XIAN, WAIT_SELLER_SEND_GOODS, 
                               WAIT_BUYER_PAY,
                               WAIT_BUYER_CONFIRM_GOODS,
                               TRADE_FINISHED, 
                               TRADE_BUYER_SIGNED,
                               TRADE_CLOSED_BY_TAOBAO,
                               TRADE_CLOSED, 
                               TRADE_NO_CREATE_PAY, nil];
        } else if (_row == 4)
        {
            /*
             type:
             交易类型列表，同时查询多种交易类型可用逗号分隔。
             默认同时查询guarantee_trade, auto_delivery, ec, cod的4种交易类型的数据 。 
             可选值： fixed(一口价)
             auction(拍卖) 
             guarantee_trade(一口价、拍卖) 
             independent_simple_trade(旺店入门版交易) 
             independent_shop_trade(旺店标准版交易) 
             auto_delivery(自动发货) 
             ec(直冲) 
             cod(货到付款) 
             game_equipment(游戏装备) 
             shopex_trade(ShopEX交易) 
             netcn_trade(万网交易) 
             external_trade(统一外部交易) 
             instant_trade (即时到账) 
             b2c_cod(大商家货到付款)
             hotel_trade(酒店类型交易)
             super_market_trade(商超交易)
             super_market_cod_trade(商超货到付款交易)
             */
            
            _pickDataSource = [[NSArray alloc] initWithObjects: 
                               BU_XIAN,
                               fixed,
                               auction,
                               guarantee_trade,
                               independent_simple_trade,
                               independent_shop_trade,
                               auto_delivery,
                               ec,
                               cod,
                               game_equipment,
                               shopex_trade,
                               netcn_trade,
                               external_trade,
                               instant_trade,
                               b2c_cod,
                               hotel_trade,
                               super_market_trade,
                               super_market_cod_trade, nil];
        }else if (_row == 5)
        {
            /*
             评价状态，默认查询所有评价状态的数据，除了默认值外每次只能查询一种状态。
             可选值：
             #define RATE_UNBUYER                @"买家未评" 
             #define RATE_UNSELLER               @"卖家未评"
             #define RATE_BUYER_UNSELLER         @"买家已评，卖家未评"
             #define RATE_UNBUYER_SELLER         @"买家未评，卖家已评"
             */
            
            _pickDataSource = [[NSArray alloc] initWithObjects: 
                               BU_XIAN, 
                               RATE_UNBUYER, 
                               RATE_UNSELLER, 
                               RATE_BUYER_UNSELLER, 
                               RATE_UNBUYER_SELLER, nil];
        }
        self.view = [viewArray objectAtIndex: 1];
    }
}

#pragma mark - View lifecycle
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    
    _tempView = [[[TempView alloc] initWithFrame: CGRectMake(0, 0, 320, 220)] autorelease];
    _tempView.backgroundColor = [UIColor colorWithRed:0.00 green:0.00 blue:0.00 alpha: 0.00];
    _tempView.searchOrderInputViewController = self;
    [appDelegate.window addSubview: _tempView];
}

- (void)viewDidUnload
{
    [self setTextPick:nil];
    [self setDatePick:nil];
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - 
- (void)cancel
{
    [self dismissModalViewControllerAnimated: YES];
    [_tempView removeFromSuperview];
}

- (IBAction)save
{
    [self cancel];
    
    if (_row == 0 || _row == 1)
    {
        [self pickedDate];
    }else if (_row == 2 || _row == 4 || _row == 5)
    {
        [self selectOrderStatus];
    }
}

- (void)pickedDate
{
    if (_delegate && [_delegate respondsToSelector: @selector(layout:row:)])
    {
        NSDate *date = [_datePick date];
        
        NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
        [dateFormatter setDateFormat: @"yyyy-MM-dd"];
        NSString *dateStr = [dateFormatter stringFromDate: date];
        
        [_delegate layout: dateStr row: _row];
        
        if (_row == 0)
        {
            LF_RELEASE_SAFELY(s_startDate);
            s_startDate = [[_datePick date] retain];
        }
    }
}

- (void)selectOrderStatus
{
    NSInteger selectRow = [_textPick selectedRowInComponent: 0];
    NSString *str = [_pickDataSource objectAtIndex: selectRow];
    [_delegate layout: str row: _row];
}

#pragma mark - UIPickerViewDelegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [_pickDataSource count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [_pickDataSource objectAtIndex: row];
}

@end
